﻿using Admin.Core.Common.Auth;
using Admin.Core.Common.Input;
using Admin.Core.Common.Output;
using Admin.Core.Model.Warehouse;
using Admin.Core.Repository.Warehouse.XbcTypeR;
using Admin.Core.Repository.Warehouse.XbcWarehouse;
using Admin.Core.Service.Warehouse.XbcTypeS.Input;
using Admin.Core.Service.Warehouse.XbcTypeS.Output;
using AutoMapper;
using System;
using System.Collections.Generic;
using System.Reflection.Emit;
using System.Text;
using System.Threading.Tasks;

namespace Admin.Core.Service.Warehouse.XbcTypeS
{
    public class XbcTypeService: IXbcTypeService
    {
        private readonly IUser _user;
        private readonly IMapper _mapper;
        private readonly IXbcTypeRepository _xbcTypeRepository;
        private readonly IXbcWarehouseRepository _xbcWarehouseRepository;
        public XbcTypeService(IUser user, IMapper mapper, IXbcTypeRepository xbcTypeRepository,IXbcWarehouseRepository xbcWarehouseRepository)
        {
            _user = user;
            _mapper = mapper;
            _xbcTypeRepository = xbcTypeRepository;
            _xbcWarehouseRepository = xbcWarehouseRepository;
        }

        /// <summary>
        /// 获取物料类型
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<IResponseOutput> GetAsync(long id)
        {
            var result = await _xbcTypeRepository.GetAsync<XbcTypeGetOutput>(id);
            return ResponseOutput.Ok(result);
        }

        public async Task<IResponseOutput> PageAsync(PageInput<XbcType> input)
        {
            var key = input.Filter?.Name;

            var list = await _xbcTypeRepository.baseRepository.Select
            .WhereIf(key.NotNull(), a => a.Name.Contains(key))
            .Count(out var total)
            .OrderByDescending(true, c => c.Id)
            .Page(input.CurrentPage, input.PageSize)
            .ToListAsync<XbcTypeListOutput>();

            var data = new PageOutput<XbcTypeListOutput>()
            {
                List = list,
                Total = total
            };
            return ResponseOutput.Ok(data);
        }

        /// <summary>
        /// 获取物料配置
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<IResponseOutput> GetTypeConfAsync(long id)
        {
            var result = await _xbcTypeRepository.GetAsync<XbcTypeConfOutput>(id);
            return ResponseOutput.Ok(result);
        }



        public async Task<IResponseOutput> AddAsync(XbcTypeAddInput input)
        {
            var entity = _mapper.Map<XbcType>(input);
            entity.TenantId = _user.TenantId;
            var id = (await _xbcTypeRepository.baseRepository.InsertAsync(entity)).Id;

            return ResponseOutput.Result(id > 0);
        }


        /// <summary>
        /// 获取物料类型的下拉
        /// </summary>
        /// <returns></returns>
        public async Task<IResponseOutput> GetXbcList()
        {
            var ls = await _xbcTypeRepository.baseRepository.Select
                  .Where(a =>a.Enabled == true)
                  .ToListAsync(a => new
                  {
                      Value = a.Id,
                      Label=  a.Name
                  });
            return ResponseOutput.Ok(ls);
        }



        public async Task<IResponseOutput> UpdateAsync(XbcTypeUpdateInput input)
        {
            if (!(input?.Id > 0))
            {
                return ResponseOutput.NotOk();
            }
            var entity = await _xbcTypeRepository.GetAsync(input.Id);
            if (!(entity?.Id > 0))
            {
                return ResponseOutput.NotOk("物料类别不存在！");
            }

            if (input.Enabled == false)
            {
                _xbcWarehouseRepository.baseRepository.Where(x => x.XbcTypeId == input.Id && x.Enabled == true).Count(out var total);
                if (total > 0)
                {
                    return ResponseOutput.NotOk("类别已经存在启用的仓库数据，不可禁用！");
                }
            }

            _mapper.Map(input, entity);
            await _xbcTypeRepository.UpdateAsync(entity);
            return ResponseOutput.Ok();
        }

        public async Task<IResponseOutput> SoftDeleteAsync(long id)
        {
            _xbcWarehouseRepository.baseRepository.Where(x => x.XbcTypeId == id).Count(out var total);
            if (total > 0)
            {
                return ResponseOutput.NotOk("类别已经存在仓库数据，不可删除！");
            }
            var result = await _xbcTypeRepository.SoftDeleteAsync(id);
            return ResponseOutput.Result(result);
        }

        public async Task<IResponseOutput> BatchSoftDeleteAsync(long[] ids)
        {
            var result = await _xbcTypeRepository.SoftDeleteAsync(ids);
            return ResponseOutput.Result(result);
        }


    }
}
